package ua.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import ua.form.RoleForm;
import ua.model.RoleListBean;


public class RoleAction extends DispatchAction{
	
	public ActionForward addRole(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response) {
		
			@SuppressWarnings("unused")
			RoleForm roleForm = (RoleForm) form;
			return mapping.findForward("showAdd");		
	}
	
	public ActionForward editRole(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response) {
		
			RoleForm roleForm = (RoleForm) form;
			String rolename = request.getParameter("rolename");  
			roleForm.setRole(RoleListBean.getRoleByName(rolename));
			return mapping.findForward("showEdit");
	}
	
	public ActionForward saveRole(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response) {
		
		    RoleForm roleForm = (RoleForm) form;
		    roleForm.save();
			return mapping.findForward("showList");

	}
	
	public ActionForward deleteRole(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response) {
			
		    RoleForm roleForm = (RoleForm) form;
		    roleForm.setId(request.getParameter("rolename"));
		    roleForm.getRole().delete();
		    return mapping.findForward("showList");	
	}
	
}
